﻿@{
    ViewBag.Title = "Management";
    Layout = "~/Views/Shared/ListLayout.cshtml";
}
<!--layout-->
<div id="cc" class="easyui-layout" data-options="fit:true,border:false">
    <div style="width: 222px;padding:5px" data-options="region:'west',split:true,title:'字典类型',collapsible:false,
         tools: [{iconCls:'icon-add',handler:addType},{iconCls:'icon-edit',handler:editType},{iconCls:'icon-remove',handler:removeType}]">
        <ul class="easyui-tree" id="ttType" data-options="url:'@Url.Action("GetAllTypeForTree")',onClick:onclickType"></ul>
    </div>
    <div data-options="region:'center',border:false">
        <table id="dg" class="easyui-datagrid" data-options="title:'字典项',fitColumns:true,pagination:true,fit:true,sortName:'DisplayOrder',toolbar:'#tb',onDblClickRow: onDblClickRow">
            <thead>
                <tr>
                    <th data-options="field:'Id',checkbox:true"></th>
                    <th data-options="field:'TypeName',width:100,sortable:true">类型名称</th>
                    <th data-options="field:'Name',width:100,editor:{type:'validatebox',options:{required:true}}">名称</th>
                    <th data-options="field:'DisplayOrder',width:100,sortable:true,editor:{type:'numberbox',options:{required:true}}">顺序</th>
                    <th data-options="field:'Remark',width:100,editor:{type:'validatebox',options:{required:false}}">备注</th>
                </tr>
            </thead>
        </table>
    </div>
</div>
<!--/layout-->
<!--toolbar-->
<div id="tb">
    <div class="datagrid-toolbar-toolField">
        <div style="width: 70%; float: left">
            @*<a href="#" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="appendItem()">添加</a>
            <a href="#" class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="deleteItem()">删除</a>
            <span class="separator"></span>
            <a href="#" class="easyui-linkbutton" iconcls="icon-save" plain="true" onclick="saveItem()">保存</a>
            <a href="#" class="easyui-linkbutton" iconcls="icon-undo" plain="true" onclick="rejectItem()">放弃</a>
            <span class="separator"></span>
            <a href="#" class="easyui-linkbutton" iconcls="icon-search" plain="true" onclick="dgSearch()">查询</a>*@
            @Html.Action("RenderToolbar", "Common", new { area = "" })
        </div>
        <div style="width: 30%; float: left;text-align:right">
            <a id="btnCollapse" href="#" class="easyui-linkbutton" iconcls="icon-collapse" plain="true"></a>
        </div>
    </div>
    <div id="pnlSearch" class="datagrid-toolbar-searchField">
        <form id="formSearch">
            名称:<input type="text" id="serhName" name="serhName" data-query="method:'Contains',name:'Name'" placeholder="名称" />
        </form>
    </div>
</div>
<!--/toolbar-->
<!--dialog-->
<div id="dlgAddType" class="easyui-dialog" title="添加字典类型" style="width:300px;height:180px;padding:10px"
     data-options="
                closed:true,
                iconCls: 'icon-save',
                toolbar: [{
                    text:'清空',
                    iconCls:'icon-reload',
                    handler:function(){
                        $('#fmType').form('clear');
                    }
                },{
                    text:'保存',
                    iconCls:'icon-save',
                    handler:saveType
                }]
            ">
    <div class="willow-form">
        <fieldset>
            <form id="fmType" class="easyui-form" method="post">
                <table class="willowTable">
                    <tr>
                        <th>
                            名称：
                        </th>
                        <td>
                            <input type="text" id="Name" name="Name" class="easyui-validatebox" data-options="required:true" />
                        </td>
                    </tr>
                    <tr>
                        <th>
                            备注：
                        </th>
                        <td>
                            <textarea id="Remark" name="Remark"></textarea>
                        </td>
                    </tr>
                </table>
                <input type="hidden" id="Id" name="Id" />
            </form>
        </fieldset>
    </div>
</div>
<!--/dialog-->
@section Script{
    <script src="~/Scripts/jquery-easyui/src/jquery.datagrid.js"></script>
    <script src="~/Scripts/jquery-easyui/src/jquery.datebox.js"></script>
    <script>
        var editIndex = undefined;
        $(function () {
            $('#btnCollapse').click(function () {
                $('#pnlSearch').toggle();
                $('#dg').datagrid('resize');
                var $collapse = $(this).find('.l-btn-text span:first');
                if ($collapse.hasClass('icon-collapse')) {
                    $collapse.removeClass('icon-collapse').addClass('icon-expand');
                } else {
                    $collapse.removeClass('icon-expand').addClass('icon-collapse');
                }
            });
        });

        //点击类型
        function onclickType(node) {
            $('#dg').datagrid({
                url: '@Url.Action("GetPagedDictionaryItem")?typeId=' + node.id
            });
        }

        //添加字典类型
        function addType() {
            $('#dlgAddType').dialog('open');
        }

        //编辑字典类型
        function editType() {
            var selected = $('#ttType').tree('getSelected');
            if (!selected) {
                top.showAlert('请选择信息!', 'warning');
                return false;
            }
            var remark = selected.attributes.remark;
            var name = selected.text;
            var id = selected.id;
            $('#Remark').val(remark);
            $('#Id').val(id);
            $('#Name').val(name);
            $('#dlgAddType').dialog('open');
        }

        //删除字典类型
        function removeType() {
            var selected = $('#ttType').tree('getSelected');
            if (!selected) {
                top.showAlert('请选择信息!', 'warning');
                return false;
            }

            $.messager.confirm('信息提示', '确定删除吗?', function (r) {
                if (r) {
                    var url = '@Url.Action("DeleteType")';
                    var postData = {
                        id: selected.id
                    };
                    common.post(url, postData, function (result) {
                        if (result) {
                            $('#ttType').tree('reload');
                        }
                    });
                }
            });
        }

        //保存类型
        function saveType() {
            var isValid = $('#fmType').form('validate');
            if (!isValid) {
                return false;
            }

            var postData = $('#fmType').form('getData');
            var url = '@Url.Action("SaveType")';
            common.post(url, postData, function (result) {
                if (result) {
                    $('#fmType').form('clear');
                    $('#ttType').tree('reload');
                    $('#dlgAddType').dialog('close');
                }
            });
        }

        /*查询*/
        function dgSearch() {
            var selectedOrg = $('#ttOrg').tree('getSelected');
            var orgId = selectedOrg == null ? null : selectedOrg.id;

            var queryParams = $('#formSearch').form('getQueryParams');
            queryParams['OrgId'] = orgId;
            $('#dg').datagrid({
                queryParams: queryParams
            });
        }

        //添加字典项
        function appendItem() {
            var selected = $('#ttType').tree('getSelected');
            if (!selected) {
                top.showAlert('请选择字典类型!', 'warning');
                return false;
            }

            var typeId = selected.id;
            var typeName = selected.text;
            if (endEditing()) {
                $('#dg').datagrid('appendRow', { TypeId: typeId, TypeName: typeName });
                editIndex = $('#dg').datagrid('getRows').length - 1;
                $('#dg').datagrid('selectRow', editIndex)
                        .datagrid('beginEdit', editIndex);
            }
        }

        //取消编辑字典项
        function rejectItem() {
            $('#dg').datagrid('rejectChanges');
            editIndex = undefined;
        }

        //结束编辑字典项
        function endEditing() {
            if (editIndex == undefined) { return true; }
            var $dg = $('#dg');
            if ($dg.datagrid('validateRow', editIndex)) {
                $dg.datagrid('endEdit', editIndex);
                editIndex = undefined;
                return true;
            } else {
                return false;
            }
        }

        //双击行
        function onDblClickRow(index) {
            if (editIndex != index) {
                if (endEditing()) {
                    $('#dg').datagrid('selectRow', index)
                            .datagrid('beginEdit', index);
                    editIndex = index;
                } else {
                    $('#dg').datagrid('selectRow', editIndex);
                }
            }
        }

        //保存字典项
        function saveItem() {

            var $dg = $("#dg");

            if (!endEditing()) { return false; }

            if ($dg.datagrid('getChanges').length == 0) { return false; }

            var inserted = $dg.datagrid('getChanges', 'inserted');
            var deleted = $dg.datagrid('getChanges', 'deleted');
            var updated = $dg.datagrid('getChanges', 'updated');
            var filterDeleted = [];
            for (var i = 0; i < deleted.length; i++) {
                if (deleted[i].Id) {
                    filterDeleted.push(deleted[i]);
                }
            }

            var postData = {
                lstInsert: inserted,
                lstDelete: filterDeleted,
                lstUpdate: updated
            };

            var url = '@Url.Action("SubmitItem")';

            common.post(url, postData, function (result) {
                if (result) {
                    $dg.datagrid('acceptChanges');
                }
            });
        }

        //删除字典项
        function deleteItem() {

            var $dg = $('#dg');

            var checkedRow = $dg.datagrid('getChecked');
            if (checkedRow.length == 0) {
                top.showAlert('请选择信息!', 'warning');
                return false;
            }

            for (var i = 0; i < checkedRow.length; i++) {
                var rowIndex = $dg.datagrid('getRowIndex', checkedRow[i]);
                if (editIndex == rowIndex) {
                    $dg.datagrid('cancelEdit', rowIndex)
                }
                $dg.datagrid('deleteRow', rowIndex);
            }
        }

    </script>
}